﻿using System.Collections;

/// <summary>
/// 主界面流程
/// </summary>
public class HomeProcedure : BaseProceduar {

    ///////////////////////////////////////////////////////////////////////////
    /// <summary>
    /// 主界面UI
    /// </summary>
    UIHomeWindow _ui;

    ///////////////////////////////////////////////////////////////////////////
    /// <summary>
    /// 单例
    /// </summary>
    static HomeProcedure _inst;
    public static HomeProcedure inst {
        get {
            if (_inst == null)
            {
                _inst = new HomeProcedure();
            }
            return _inst;
        }
    }

    ///////////////////////////////////////////////////////////////////////////

    public override void EnterProceduar(bool initUI)
    {
        DebugInfo.Log("进入主界面流程");

        if (initUI)
        {
            // 刷新数据
            ReqUserInfo();
        }
    }

    public override void UpdateProceduar()
    {
        base.UpdateProceduar();
    }

    public override void LeaveProceduar()
    {
        DebugInfo.Log("退出主界面流程");
    }

    protected override void HandleMessage(Message message)
    {
        switch(message.msgId)
        {
            case Commands.CMD_CTRL_BUTTON_CLICKED:
                switch(message.iParam)
                {
                    default:
                        break;
                }
                break;

            case Commands.CMD_MOD_MSG:
                switch(message.iParam)
                {
                    case GameCommands.CMD_JOIN_ROOM:
                        UIRootController.inst.ShowMask(true);
                        // 请求加入房间
                        GameDataKernel.inst.roomId = (int)message.oParam;
                        // 切换到游戏流程
                        GameController.inst.SwitchTo(GameProceduar.inst);
                        break;
                }
                break;

            default:
                base.HandleMessage(message);
                break;
        }
    }

    protected override bool ProcessPacket(IOMessage message)
    {
        switch(message.code)
        {
            case IOCmds.SMSG_LOGIN:
                UIRootController.inst.ShowMask(false);

                if (message.state == IOStates.STATE_OK)
                {
                    // 登录成功
                    HashtableEx args = message.args;

                    UserInfo userInfo = new UserInfo();
                    userInfo.uid = args.GetInt("uid");

                    HashtableEx infoArgs = args.GetMapEx("info");
                    userInfo.name = infoArgs.GetString("name");
                    userInfo.headpic = infoArgs.GetString("headpic");
                    userInfo.sex = infoArgs.GetInt("sex");

                    // 用户信息加载完成
                    DataKernel.inst.userInfo = userInfo;

                    // TODO 从服务器读取消息
                    DataKernel.inst.annInfo.addAnn("小小跑胡子开启内测");
                    DataKernel.inst.annInfo.addAnn("小小跑胡子年前公测正式开启");
                    DataKernel.inst.annInfo.addAnn("庆祝小小跑胡子运营100周年");

                    // TODO 从服务器读取公告
                    DataKernel.inst.noticeStr = "我是公告！";

                    InitUI();
                }
                return true;

            case IOCmds.SMSG_CREATE_ROOM:
                UIRootController.inst.ShowMask(false);
                if (message.state == IOStates.STATE_OK)
                {
                    UIRootController.inst.ShowMask(true);

                    // 请求加入房间
                    GameDataKernel.inst.roomId = message.args.GetInt("roomId");
                    // 切换到游戏流程
                    GameController.inst.SwitchTo(GameProceduar.inst);
                }
                return true;
        }
        return base.ProcessPacket(message);
    }

    /// <summary>
    /// 初始化主界面UI
    /// </summary>
    void InitUI()
    {
        _ui = new UIHomeWindow();
        _ui.UpdateUserInfo();

        UIRootController.inst.SwitchTo(_ui);

        if (!LocalStorage.HasKey("弹出公告"))
        {
            // 弹出公告
            UINoticeWindow.Popup();
            LocalStorage.SetInt("弹出公告", 1);
        }

        DebugInfo.Log("主界面初始化完毕");
    }

    /// <summary>
    /// 发送登录请求
    /// </summary>
    void ReqUserInfo()
    {
        // 发送登录请求
        UIRootController.inst.ShowMask(true);
        NetIOController.inst.SendMessage(DataKernel.inst.loginReqInfo.ToMessage(IOCmds.CMSG_LOGIN));
    }
}
